﻿using System.Collections;
using System.ComponentModel;
using WPFDBBrowser.Constants;
using WPFDBBrowser.Framework;
using WPFDBBrowser.Interfaces;
using WPFDBBrowser.Interfaces.DBTreeView;
using WPFDBBrowser.MessageBus.Messages;
using WPFDBBrowser.Settings;

namespace WPFDBBrowser.UserControls
{
    public class DBTreeViewPresenter : PresenterBase<IDBTreeView>
    {
        public DBTreeViewPresenter()
        {
            this.Connections = new BindingList<DBEntity>();
        }

        private BindingList<DBEntity> Connections { get; set; }

        public IEnumerable GetConnections()
        {
            return this.Connections;
        }

        public void AddConnection(DBEntity newConnection)
        {
            this.Connections.Add(newConnection);
        }

        public override void OnViewLoaded()
        {
            base.OnViewLoaded();

            MasterDBSettings settings = new MasterDBSettings();
            settings.LoadSettings();
            foreach (DBConnectionInfo info in settings.DBsInfos)
            {
                this.Connections.Add(new DBEntity(info.Type)
                {
                    ConnectionString = info.ConnectionString,
                    Name = info.Name
                });
            }
        }

        public override void OnViewClosed()
        {
            base.OnViewClosed();

            if (this.Connections.Count > 0)
            {
                MasterDBSettings settings = new MasterDBSettings();
                foreach (DBEntity entity in this.Connections)
                {
                    settings.DBsInfos.Add(new DBConnectionInfo(entity.ConnectionString, entity.Name, entity.Type));
                }
                settings.SaveSettings();
            }
        }
        protected override string GetIdentification()
        {
            return ComponentNames.DBTREEVIEW_NAME;
        }

        public void LoadTables(DBEntity entity)
        {
            IDBFactory factory = this.UnityContainer.Resolve<IDBFactory>(entity.Type);
            entity.SetTables(factory.GetTables(entity.ConnectionString));
            if (!entity.IsExpanded)
                entity.IsExpanded = true;
        }

        public void LoadViews(DBEntity entity)
        {
            IDBFactory factory = this.UnityContainer.Resolve<IDBFactory>(entity.Type);
            entity.SetViews(factory.GetViews(entity.ConnectionString));
        }
        public void LoadColumns(IColumnContainer tableOrView)
        {
            IDBFactory factory = this.UnityContainer.Resolve<IDBFactory>(tableOrView.Type);
            DBEntity entity = tableOrView.GetParentNode<DBEntity>();
            if (entity != null)
            {
                tableOrView.SetColumns(factory.GetColumns(entity.ConnectionString, tableOrView.Name));
                if (!tableOrView.IsExpanded)
                    tableOrView.IsExpanded = true;
            }
        }
        public void RunQuery(IColumnContainer tableOrView, bool onlyCopy)
        {
            try
            {
                IDBFactory factory = this.UnityContainer.Resolve<IDBFactory>(tableOrView.Type);
                // FIXME: hardcoded value for number of rows
                base.PublishMessage<QueryMsg>(new QueryMsg() { Query = factory.GetQuery(tableOrView, 50), OnlyCopy = onlyCopy });
            }
            catch
            {
                // FIXME: exception handling
            }
        }
        public void RunViewQuery(IView view, bool onlyCopy)
        {
            try
            {
                IDBFactory factory = this.UnityContainer.Resolve<IDBFactory>(view.Type);
                // FIXME: hardcoded value for number of rows
                base.PublishMessage<QueryMsg>(new QueryMsg() { Query = factory.PrepareViewQuery(view, 50), OnlyCopy = onlyCopy });
            }
            catch
            {
                // FIXME: exception handling
            }
        }
    }
}
